< previous page page_116 next page >

Page 116
Using a Module Variable
Heres a sample exercise that shows how to use a module-level variable. Lets take a look at this exercise.
In the Form_Load() event procedure of VarsExamples.frm youll see a variable named mstrName. It has been assigned the value Bill Gates. You cannot give a variable an initial value when you declare it; therefore, the Form_Load() procedure is a good place to set module-level variables to a valid starting value:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub Form_Load ()
   mstrName = "Bill Gates"
End Sub
Because the Form_Load() procedure is part of this module, you can both read and write to the mstrName variable. Lets now read and change this value from another event procedure in this module. If you double-click the Module Var command button, youll see the following code:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdModule_Click ()
   lblModule.Caption = mstrName

   mstrName = "Tom Buttons"
End Sub
EXERCISE
Check Out the Module-level Variables Example
1. Run the sample project.
2. Click the Variables command button to run the form VarExamples.frm.
3. Click the Module Var command button twice to see the two values change in the label next to the button.

In the cmdModule_Click() event, you retrieve the value from mstrName and place it in the Caption property of the label. You then change the value of mstrName to Tom Buttons, so the next time you click this button, the value in the label control will change. If you were to create other event procedures, or even your own user-defined procedures (youll learn to create these later) within this module, you would be able to read and change the mstrName variable from any of them.

 
< previous page page_116 next page >

If you like this book, buy it!